package com.by.controller;

import com.by.bean.Enterprise;
import com.by.bean.XuFei;
import com.by.bean.query.EnterpriseQuery;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.valid.ISave;
import com.by.common.valid.IUpdate;
import com.by.service.EnterpriseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api")
@Api(tags = "企业管理")
/*@CrossOrigin*/
public class EnterpriseController extends BaseController {

    @Autowired
    EnterpriseService enterpriseService;


    @ApiOperation("保存企业")
    @PostMapping("/enterprise")
    public ResultBean save(@Validated(ISave.class) @RequestBody Enterprise enterprise, BindingResult result) {
        validate(result);
        int rs = enterpriseService.save(enterprise);
        return saveResult(rs);
    }

    @ApiOperation("企业列表")
    @PostMapping("/enterprise/list")
    public ResultBean<PageBean> query(@RequestBody EnterpriseQuery query) {
        PageBean pageBean = enterpriseService.query(query.toMap());
        return success("企业列表", pageBean);
    }

    @ApiOperation("修改企业")
    @PutMapping("/enterprise")
    public ResultBean update(@Validated(IUpdate.class) @RequestBody Enterprise enterprise, BindingResult result) {
        int rs = enterpriseService.update(enterprise);
        return updateResult(rs);
    }


    @ApiImplicitParam(name = "id", value = "企业id", required = true, dataTypeClass = Integer.class)
    @ApiOperation("删除企业")
    @DeleteMapping("/enterprise")
    public ResultBean delete(@RequestParam(name = "id", required = true) int id) {
        int rs = enterpriseService.delete(id);
        return deleteResult(rs);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "企业id", required = true, dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "empNum", value = "可创建的员工数", required = true, dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "accountNum", value = "可创建的账号数", required = true, dataTypeClass = Integer.class),
    })
    @PutMapping("/enterprise/auth")
    @ApiOperation("权限")
    public ResultBean auth(@RequestParam(name = "id", required = true) int id, int empNum, int accountNum) {
        int rs = enterpriseService.auth(id, empNum, accountNum);
        return saveResult(rs);
    }


    @ApiOperation("续费")
    @PutMapping("/enterprise/xufei")
    public ResultBean xufei(@Validated @RequestBody XuFei xuFei, BindingResult result) {
        validate(result);
        int rs = enterpriseService.xufei(xuFei);
        return rs == 1 ? success("续费成功") : fail("续费失败");
    }

    @ApiOperation("企业资金信息")
    @GetMapping("/enterprise/{id}")
    public ResultBean get(@PathVariable(name = "id") int id) {
        Map map = enterpriseService.queryMoney(id);
        return success("资金信息", map);
    }


    @ApiOperation("导出企业")
    @PostMapping("/enterprise/export")
    public ResultBean export(@RequestBody Map map) {
        String url = enterpriseService.export(map);
        return success("导出企业", url);
    }

}
